package com.liauto.timelist.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.navigation.fragment.NavHostFragment.Companion.findNavController
import androidx.viewbinding.ViewBinding
import com.liauto.timelist.R


abstract class BaseFragment<B : ViewBinding, M : ViewModel> : Fragment() {

    lateinit var binding: B
    lateinit var viewModel: M

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = provideViewBinding(inflater, container, savedInstanceState)
        return binding.root
    }

    abstract fun provideViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): B

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        viewModel = provideViewModel()
        initEvent()
    }

    abstract fun provideViewModel(): M

    abstract fun initEvent()

    protected fun replaceTimeFragment(hour: Int, min: Int) {
        findNavController(this).navigate(R.id.action_listFragment_to_timeFragment, Bundle().apply {
            putInt("hour", hour)
            putInt("min", min)
        })
    }

    protected fun backToListFragment() {
        findNavController(this).navigate(R.id.action_timeFragment_to_listFragment)
    }


}